'use strict';
angular.module('purchase.purchaseOrderDetailSelectMulti', ['app.services'])
	.controller('purchaseOrderDetailSelectMultiCtrl'
			,['$rootScope', '$scope','systemService', 'BizData', '$uibModalInstance','params', 'title'
			   , f])//控制器结束
    function f($rootScope, $scope, systemService, BizData, $uibModalInstance, params, title) {

		var Model = this;

		Model.gridRowClick = function(event){
			
		}

		Model.gridRowDblClick = function(event){
			Model.ok();
		}
		
		var options = {};
		options.Model = Model;
		options.httpUrl = $rootScope.getServiceName();
		options.reader = '/purchase/orderDetailQuery/list/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.defCols = {fID:{name:"fID",type:"String",label:""},systemCode:{name:"systemCode",type:"String",label:""},billType:{name:"billType",type:"String",label:""},billTypeName:{name:"billTypeName",type:"String",label:""},orderDate:{name:"orderDate",type:"Date",label:""},state:{name:"state",type:"String",label:""},status:{name:"status",type:"String",label:""},audit:{name:"audit",type:"String",label:""},confirmDate:{name:"confirmDate",type:"Date",label:""},origin:{name:"origin",type:"String",label:""},originName:{name:"originName",type:"String",label:""},originNo:{name:"originNo",type:"String",label:""},originSerialNo:{name:"originSerialNo",type:"String",label:""},stockNo:{name:"stockNo",type:"String",label:""},factoryID:{name:"factoryID",type:"String",label:""},factoryNo:{name:"factoryNo",type:"String",label:""},factoryName:{name:"factoryName",type:"String",label:""},storageTypeD:{name:"storageTypeD",type:"String",label:""},storageTypeDLabel:{name:"storageTypeDLabel",type:"String",label:""},providerID:{name:"providerID",type:"String",label:""},providerNo:{name:"providerNo",type:"String",label:""},shortName:{name:"shortName",type:"String",label:""},providerName:{name:"providerName",type:"String",label:""},storageType:{name:"storageType",type:"String",label:""},storageTypeLabel:{name:"storageTypeLabel",type:"String",label:""},storageID:{name:"storageID",type:"String",label:""},storageNo:{name:"storageNo",type:"String",label:""},storageName:{name:"storageName",type:"String",label:""},floorNo:{name:"floorNo",type:"String",label:""},taxType:{name:"taxType",type:"Integer",label:""},taxTypeLabel:{name:"taxTypeLabel",type:"String",label:""},taxRate:{name:"taxRate",type:"Decimal",label:""},serialNo:{name:"serialNo",type:"String",label:""},fMasterID:{name:"fMasterID",type:"String",label:""},checkin:{name:"checkin",type:"Integer",label:""},itemID:{name:"itemID",type:"String",label:""},itemNo:{name:"itemNo",type:"String",label:""},itemName:{name:"itemName",type:"String",label:""},itemProperty:{name:"itemProperty",type:"String",label:""},itemDescribe:{name:"itemDescribe",type:"String",label:""},unitCode:{name:"unitCode",type:"String",label:""},unitName:{name:"unitName",type:"String",label:""},qualityType:{name:"qualityType",type:"Integer",label:""},qualityTypeLabel:{name:"qualityTypeLabel",type:"String",label:""},batchNo:{name:"batchNo",type:"String",label:""},qualityPeriod:{name:"qualityPeriod",type:"Integer",label:""},validDays:{name:"validDays",type:"Integer",label:""},checkoutAgainDays:{name:"checkoutAgainDays",type:"Integer",label:""},storeNo:{name:"storeNo",type:"String",label:""},images:{name:"images",type:"String",label:""},imageurl:{name:"imageurl",type:"String",label:""},process:{name:"process",type:"String",label:""},processLabel:{name:"processLabel",type:"String",label:""},itemPrice:{name:"itemPrice",type:"Decimal",label:""},itemAmount:{name:"itemAmount",type:"Decimal",label:""},orderAmount:{name:"orderAmount",type:"Decimal",label:""},orderAmountOld:{name:"orderAmountOld",type:"Decimal",label:""},orderAmountTax:{name:"orderAmountTax",type:"Decimal",label:""},orderAmountTotal:{name:"orderAmountTotal",type:"Decimal",label:""},piecePackage:{name:"piecePackage",type:"Decimal",label:""},pieceQuantity:{name:"pieceQuantity",type:"Decimal",label:""},requirementDate:{name:"requirementDate",type:"Date",label:""},storeItemIng:{name:"storeItemIng",type:"Decimal",label:""},storeItemAmount:{name:"storeItemAmount",type:"Decimal",label:""},storePiece:{name:"storePiece",type:"Decimal",label:""},backItemAmount:{name:"backItemAmount",type:"Decimal",label:""},backPiece:{name:"backPiece",type:"Decimal",label:""},pendingQuantity:{name:"pendingQuantity",type:"Decimal",label:""},pendingPiece:{name:"pendingPiece",type:"Decimal",label:""},itemWeight:{name:"itemWeight",type:"Decimal",label:""},itemSize:{name:"itemSize",type:"String",label:""},orderWeight:{name:"orderWeight",type:"Decimal",label:""},orderSize:{name:"orderSize",type:"String",label:""},projectNo:{name:"projectNo",type:"String",label:""},orderNo:{name:"orderNo",type:"String",label:""},resultStatus:{name:"resultStatus",type:"String",label:""},employeeNo:{name:"employeeNo",type:"String",label:""},employeeName:{name:"employeeName",type:"String",label:""},comments:{name:"comments",type:"String",label:""},userId:{name:"userId",type:"String",label:""},enterpriseNo:{name:"enterpriseNo",type:"String",label:""},enterpriseName:{name:"enterpriseName",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
//		options.master = {"data":"$scope.dataMaster","relation":"fMasterID"};
//		options.masterData = $scope.dataMaster;
//		options.slaveDatas = null;
		options.directDeleteMode = false;
		options.orderBy = 'stockNo,serialNo ASC';
		options.limit = 10;
		
		$scope.dataList = new BizData(options);

		////////////////////启动执行开始///////////////////////////////////////////////////
		//接收调用的参数
//		Model.params = $rootScope.$stateParams;  
		////////////////////启动执行结束///////////////////////////////////////////////////
		
		//////////////////////对话框使用开始//////////////////////////////////////
		
        //模态窗口打开之后执行的事件
        $uibModalInstance.opened.then(function(){
    		//接收调用对话框的参数
        	Model.title = title;  
        	Model.params = params;  

    		//设置过滤条件
    		$scope.dataList.filter = "PurchaseOrderDetail.enterpriseNo = '"+ Model.params.enterpriseNo +"'"
    		+ " AND PurchaseOrderDetail.status='open' "
    		+ " AND PurchaseOrderDetail.audit='confirm' "
    		+ " AND (PurchaseOrderDetail.itemAmount - PurchaseOrderDetail.storeItemIng - PurchaseOrderDetail.storeItemAmount)>0 "
    		+ " AND PurchaseOrderDetail.factoryID = '"+ Model.params.factoryID +"'"
    		+ " AND PurchaseOrderDetail.providerID = '"+ Model.params.id +"'"
    		;
//    		console.log('Model.params.filter-->',Model.params.filter);
    		if(Model.params.filter!=undefined && Model.params.filter!=''){
    			$scope.dataList.filter = $scope.dataList.filter + " AND " + Model.params.filter;
    		}
//    		console.log('$scope.dataList.filter-->',$scope.dataList.filter);
//    		$scope.dataList.filter = '';

    		//刷新数据
    	    $scope.dataList.refreshData();
        });  
		
        Model.ok = function () {  
	    	//保存成功
	    	//打包数据
	    	var event = {};
	    	var data = [];
	    	
	    	//循环data
	    	for(var index=0;index<$scope.dataList.rows.length;index++){
	    		if($scope.dataList.rows[index].userdata.checked){
	    	    	
	    	    	var sourceRow = $scope.dataList.rows[index];
	    	    	var row = {};
	    			//循环行中的列，取出列的值，装配成简单的行
	    			for(var obj in sourceRow){
	    				if(obj!='$$hashKey' && obj!='userdata'){
	    					row[obj] = sourceRow[obj].value;
	    				}
	    			}
	    	    	
	    	    	data.push(row);
	    			
	    		}
	    	}
	    	
	    	event.data = data;
			$uibModalInstance.close(event);  
	    };  
	    
	    Model.cancel = function () {  
	        $uibModalInstance.dismiss('cancel');  
	    };    

	  //表格标题中的选择框ng-model
	  $scope.checkboxValue = false;
	  //表格标题中的选择对话框click事件
	  Model.checkboxClick = function () {  
	  	for(var index=0;index<$scope.dataList.rows.length;index++){
	  		$scope.dataList.rows[index].userdata.checked=$scope.checkboxValue;
	  	}
	  };    

	  //////////////////////对话框使用结束//////////////////////////////////////

	    return Model;
	    
	}	
	
